package org.linlinjava.litemall.db.service;

import com.alibaba.druid.util.StringUtils;
import com.github.pagehelper.PageHelper;
import org.linlinjava.litemall.db.dao.LitemallCouponMapper;
import org.linlinjava.litemall.db.domain.LitemallCoupon;
import org.linlinjava.litemall.db.domain.LitemallCouponExample;
import org.linlinjava.litemall.db.util.CouponConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class LitemallCouponServiceImpl implements LitemallCouponService {

    @Resource
    LitemallCouponMapper mapper;

    @Override
    public List<LitemallCoupon> getAllBytitleOrTypeOrStatus(String title, Short type, Short Status,
                                                            Integer page,Integer pageSize,
                                                            String sort,String order) {
        LitemallCouponExample example = new LitemallCouponExample();
        LitemallCouponExample.Criteria criteria = example.createCriteria();
        PageHelper.startPage(page,pageSize);
        criteria.andDeletedEqualTo(false);
        if(title != null) {
            criteria.andNameLike('%' + title + '%');
        }
        if(type != null) {
            criteria.andTypeEqualTo(type);
        }
        if(Status != null) {
            criteria.andStatusEqualTo(Status);
        }

        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }

        return mapper.selectByExample(example);
    }

    @Override
    public boolean add(LitemallCoupon coupon) {
        coupon.setDeleted(false);
        coupon.setAddTime(new Date());
        return mapper.insert(coupon) > 0 ? true : false;
    }

    @Override
    public boolean modify(LitemallCoupon coupon) {
        coupon.setUpdateTime(new Date());
        return mapper.updateByPrimaryKey(coupon) > 0 ? true : false;
    }

    @Override
    public boolean delete(LitemallCoupon coupon) {
        coupon.setDeleted(true);
        coupon.setUpdateTime(new Date());
        return mapper.updateByPrimaryKey(coupon) > 0 ? true : false;
    }

    @Override
    public LitemallCoupon getCouponById(Integer id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public List<LitemallCoupon> queryList(int offset,int limit){
        LitemallCouponExample example = new LitemallCouponExample();
        example.setOrderByClause("add_time" + " " + "desc");
        PageHelper.startPage(offset,limit);
        return mapper.selectByExample(example);
    }

    public List<LitemallCoupon> queryAvailableList(Integer userId, int offset, int limit){
        LitemallCouponExample example = new LitemallCouponExample();
        example.setOrderByClause("add_time" + " " + "desc");
        PageHelper.startPage(offset,limit);
        return mapper.selectByExample(example);
    }

    /**
     * 查询，空参数
     *
     * @param offset
     * @param limit
     * @param sort
     * @param order
     * @return
     */
    public List<LitemallCoupon> queryList(int offset, int limit, String sort, String order) {
        LitemallCouponExample example = new LitemallCouponExample();
        example.setOrderByClause("add_time" + " " + "desc");
        PageHelper.startPage(offset,limit);
        return mapper.selectByExample(example);
    }

    public LitemallCoupon findById(Integer id) {
        return mapper.selectByPrimaryKey(id);
    }

    public LitemallCoupon findByCode(String code) {
        LitemallCouponExample example = new LitemallCouponExample();
        example.or().andCodeEqualTo(code).andTypeEqualTo(CouponConstant.TYPE_CODE).andStatusEqualTo(CouponConstant.STATUS_NORMAL).andDeletedEqualTo(false);
        List<LitemallCoupon> couponList =  mapper.selectByExample(example);
        if(couponList.size() > 1){
            throw new RuntimeException("");
        }
        else if(couponList.size() == 0){
            return null;
        }
        else {
            return couponList.get(0);
        }
    }

}